<template>
  <div class="home-box" style="margin-left: 20px">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="员工姓名">
        <el-input
          v-model="searchForm.empName"
          placeholder="员工姓名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="岗位名称">
        <el-input
          v-model="searchForm.position"
          placeholder="岗位名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-button
    type="primary"
    style="margin-left: 20px"
    plain
    icon="Plus"
    @click="handleAdd"
    >新增</el-button
  >
  <el-button
    color="#626aef"
    :dark="isDark"
    plain
    icon="uploadload"
    @click="handleUP"
    >导入</el-button
  >
  <el-button
    color="#ff983f"
    :dark="isDark"
    plain
    icon="Download"
    @click="handleOut"
    >导出</el-button
  >
  <el-table
    :data="PositionList"
    :span-method="(value) => rowSpanRulesHandler(value, PositionList)"
    style="
      width: 95%;
      margin: 20px;
      border-radius: 5px;
      box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
    "
    size="large"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
    }"
  >
    <el-table-column
      prop="empName"
      label="员工姓名"
      width="120"
      class-name="my-content"
    >
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #default>
            <div>备注：{{ scope.row.ins }}</div>
          </template>
          <template #reference>
            {{ scope.row.empName }}
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column prop="position" label="岗位" width="120" />
    <el-table-column prop="deptName" label="所属部门" width="120" />
    <el-table-column prop="posiPercent" label="绩效占比" width="120" />
    <el-table-column prop="state" label="状态" width="80">
      <template #default="scope">
        <el-tag
          :type="scope.row.state === 1 ? 'success' : 'dange'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column prop="auditStatus" label="考核状态" width="110">
      <template #default="scope" >
        <el-tag
		   v-if="scope.row.state == 1"
          :type="
            scope.row.auditStatus === 0
              ? 'info'
              : scope.row.auditStatus === 1
              ? 'primary'
              : scope.row.auditStatus === 2
              ? 'warning'
              : scope.row.auditStatus === 3
              ? 'success'
              : 'danger'
          "
          disable-transitions
          >{{
            scope.row.auditStatus === 0
              ? "未考核"
              : scope.row.auditStatus === 1
              ? "考核中"
              : scope.row.auditStatus === 2
              ? "暂停"
              : scope.row.auditStatus === 3
              ? "考核完成"
              : "未知"
          }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          @click="handleEdit(scope.$index, scope.row)"
          round
          v-show="scope.row.auditStatus === 0"
          >编辑</el-button
        >
        <el-button
          round
          type="danger"
          @click="handleDelete(scope.$index, scope.row)"
          v-show="scope.row.auditStatus === 0"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="PositionPage.total > 0"
    :total="PositionPage.total"
    v-model:page="searchForm.page"
    v-model:limit="PositionPage.size"
    @pagination="handlePagination"
  />
  <Dialog
    v-model="dialog_2Visible"
    :close="close"
    :update="update"
    :editForm="editForm"
    :title="title"
    :type="type"
  ></Dialog>
  <UploadExcel
    v-model="upload.open"
    :title="upload.upLoadTitle"
    :url="importUrl"
    @onSuccess="onSuccess"
    @onDownTemplate="onDownTemplate"
  ></UploadExcel>
</template>

<script setup>
import { ref, onMounted, onBeforeMount, reactive, watch } from "vue";
import { Timer } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import Dialog from "./components/Dialog";
import {
  getEmpPosition,
  deleteEmpPosition,
  getEmpPositionSearch,
} from "@/api/position";
import { ElMessage } from "element-plus";
import UploadExcel from "@/components/SendAndDownload/index";
import { getEmpPositionTemplate } from "@/api/template";
import { downLoadEmployeePosition } from "@/api/downExcel";

const searchForm = ref({
  empName: "",
  position: "",
  page: 1,
  page_size: 10,
});
const PositionPage = ref([]);
const PositionList = ref([]);
onBeforeMount(() => {
  getEmpPosition(searchForm.value).then((data) => {
    PositionPage.value = data.data;
    PositionList.value = data.data.records;
  });
});
watch(PositionList, (newVal) => {}, { deep: true });

const importUrl = ref("/EmployeePosition/upload");
const upload = ref({
  open: false,
  upLoadTitle: "员工岗位上传",
});
const onSuccess = (res) => {
  getEmpPosition(searchForm.value).then((data) => {
    PositionPage.value = data.data;
    PositionList.value = data.data.records;
  });
  upload.value.open = false;
};
const handleUP = () => {
  upload.value.open = true;
};
const onDownTemplate = () => {
  getEmpPositionTemplate();
};

const handleOut = () => {
  downLoadEmployeePosition();
};

const compareSingleAttrReturnColSpan = (row, compareAttr, index, tableData) => {
  let count = 0;
  let start = index;
  let multCompare = true;

  while (start < tableData.length && multCompare) {
    start++;
    count++;
    if (start <= tableData.length - 1)
      multCompare = row.empId === tableData[start].empId;
    else multCompare = row.empId === tableData[start - 1].empId;
  }
  start--;

  if (start < tableData.length) {
    tableData[index][`${compareAttr}_count`] = count;
  }

  if (index > 0 && count + 1 === tableData[index - 1][`${compareAttr}_count`]) {
    return [0, 0];
  }
  return [count, 1];
};

const columns = [
  "name",
  "position",
  "deptName",
  "posi_percnet",
  "state",
  "auditStatus",
  "operate",
];
const rowSpanRulesHandler = ({ row, column, rowIndex, columnIndex }, data) => {
  //console.log(columns[columnIndex]);
  if (columns[columnIndex] === "name" || columns[columnIndex] === "operate") {
    const r = compareSingleAttrReturnColSpan(row, "emp_id", rowIndex, data);
    //console.log(r);
    return r;
  }
};

const title = ref("新增员工岗位");
const type = ref("add");
const dialog_2Visible = ref(false);
const editForm = ref([]);
const handleAdd = (index, row) => {
  title.value = "新增员工岗位";
  type.value = "add";
  dialog_2Visible.value = true;
  editForm.value = [];
  editForm.value = {
    empName: "",
    empId: "",
  };
};
const handleEdit = (index, row) => {
  title.value = "编辑员工岗位";
  type.value = "edit";
  dialog_2Visible.value = true;
  //editForm.value.splice(0, editForm.length)
  editForm.value = row;
};
const handleDelete = (index, row) => {
  deleteEmpPosition(row).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      update();
    } else {
      ElMessage.error("删除失败");
    }
  });
};

const handlePagination = (value) => {
  if (searchForm.value.empName == "" && searchForm.value.position == "") {
    getEmpPosition(value).then((data) => {
      PositionPage.value = data.data;
      PositionList.value = data.data.records;
    });
  } else {
    searchForm.value.page = value.page;
    searchForm.value.page_size = value.page_size;
    getEmpPositionSearch(searchForm.value).then((data) => {
      PositionPage.value = data.data;
      PositionList.value = data.data.records;
    });
  }
};

const searchEvent = () => {
  getEmpPositionSearch(searchForm.value).then((data) => {
    PositionPage.value = data.data;
    PositionList.value = data.data.records;
  });
};

const close = () => {
  dialog_2Visible.value = false;
};
const update = () => {
  getEmpPosition(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "更新成功",
        type: "success",
      });
      PositionPage.value = data.data;
      PositionList.value = data.data.records;
    } else {
      ElMessage.error("更新失败");
    }
  });
};
</script>

<style lang="scss" scoped>
:deep(.my-content) {
  background: #ecf5ff !important;
  font-size: 15px;
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>
